package com.xzqwjw.taskmanager.adapter;

import java.util.ArrayList;
import java.util.List;

import com.leaderinfo.model.UnitNode;
import com.leaderinfo.leaderinfojxyz.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.view.View.OnClickListener;

/**
 * ������Դ������
 */
public class UnitNodeCBAdapter extends BaseAdapter {

	@SuppressWarnings("unused")
	private Context objContext;
	private com.leaderinfo.utility.UnitNodeCBAdapter objThis = this;
	private LayoutInflater objLInflater;
	private List<UnitNode> listAllNodeCache = new ArrayList<UnitNode>();
	private List<UnitNode> listAllNode = new ArrayList<UnitNode>();
	// �Ƿ�ӵ�е�ѡ��
	private boolean boolHasRadioButton = false;
	// �Ƿ�ӵ�и�ѡ��
	private boolean boolHasCheckBox = true;

	private int intIconExpanded = -1;
	private int intIconCollapsed = -1;

	/**
	 * TreeAdapter���캯��
	 * 
	 * @param objContext
	 * @param nodeRoot
	 *            ���ڵ�
	 */
	public UnitNodeCBAdapter(Context objContext, UnitNode nodeRoot) {
		this.objContext = objContext;
		this.objLInflater = (LayoutInflater) objContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		addNode(nodeRoot);
	}

	private void addNode(UnitNode objNode) {
		listAllNode.add(objNode);
		listAllNodeCache.add(objNode);
		if (objNode.isLeaf())
			return;
		for (int i = 0; i < objNode.getChildren().size(); i++) {
			addNode(objNode.getChildren().get(i));
		}
	}

	// ��ѡ������
	private void checkCheckBox(UnitNode objNode, boolean boolIsChecked) {
		objNode.setChecked(boolIsChecked);
		for (int i = 0; i < objNode.getChildren().size(); i++) {
			checkCheckBox(objNode.getChildren().get(i), boolIsChecked);
		}
	}

	/**
	 * ���ѡ�нڵ�
	 */
	public List<UnitNode> getSeletedNodes() {
		List<UnitNode> listNodes = new ArrayList<UnitNode>();
		for (int i = 0; i < listAllNodeCache.size(); i++) {
			UnitNode objNode = listAllNodeCache.get(i);
			if (objNode.isChecked()) {
				listNodes.add(objNode);
			}
		}
		return listNodes;
	}

	// ���ƽڵ��չ�����۵�
	private void filterNode() {
		listAllNode.clear();
		for (int i = 0; i < listAllNodeCache.size(); i++) {
			UnitNode objNode = listAllNodeCache.get(i);
			if (!objNode.isParentCollapsed() || objNode.isRoot()) {
				listAllNode.add(objNode);
			}
		}
	}

	/**
	 * �����Ƿ�ӵ�е�ѡ��
	 */
	public void setRadioButton(boolean boolHasRadioButton) {
		this.boolHasRadioButton = boolHasRadioButton;
	}

	/**
	 * �����Ƿ�ӵ�и�ѡ��
	 */
	public void setCheckBox(boolean boolHasCheckBox) {
		this.boolHasCheckBox = boolHasCheckBox;
	}

	/**
	 * ����չ�����۵�״̬ͼ��
	 * 
	 * @param intIconExpanded
	 *            չ��ʱͼ��
	 * @param intIconCollapsed
	 *            �۵�ʱͼ��
	 */
	public void setExpandedCollapsedIcon(int intIconExpanded,
			int intIconCollapsed) {
		this.intIconExpanded = intIconExpanded;
		this.intIconCollapsed = intIconCollapsed;
	}

	/**
	 * ����չ������
	 * 
	 * @param intLevel
	 */
	public void setExpandLevel(int intLevel) {
		listAllNode.clear();
		for (int i = 0; i < listAllNodeCache.size(); i++) {
			UnitNode objNode = listAllNodeCache.get(i);
			if (objNode.getLevel() <= intLevel) {
				if (objNode.getLevel() < intLevel) {
					// �ϲ㶼����չ��״̬
					objNode.setExpanded(true);
				} else {
					// ���һ�㶼�����۵�״̬
					objNode.setExpanded(false);
				}
				listAllNode.add(objNode);
			}
		}
		this.notifyDataSetChanged();
	}

	/**
	 * ���ƽڵ��չ��������
	 * 
	 * @param intPosition
	 */
	public void ExpandOrCollapse(int intPosition) {
		UnitNode objNode = listAllNode.get(intPosition);
		if (objNode != null) {
			if (!objNode.isLeaf()) {
				objNode.setExpanded(!objNode.isExpanded());
				filterNode();
				this.notifyDataSetChanged();
			}
		}
	}

	@Override
	public int getCount() {
		if (listAllNode == null)
			return 0;
		else
			return listAllNode.size();
	}

	@Override
	public Object getItem(int intPosition) {
		if (listAllNode == null)
			return null;
		else
			return listAllNode.get(intPosition);
	}

	@Override
	public long getItemId(int intPosition) {
		return intPosition;
	}

	@Override
	public View getView(int intPosition, View objView, ViewGroup vgParent) {

		if (listAllNode == null || getCount() == 0) {
			return null;
		}

		ViewHolder objViewHolder = null;

		if (objView == null) {
			objView = this.objLInflater.inflate(R.layout.item_unit_tree, null);

			objViewHolder = new ViewHolder();
			objViewHolder.cbSelect = (CheckBox) objView
					.findViewById(R.id.cbSelect);
			// ��ѡ�򵥻��¼�
			objViewHolder.cbSelect.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View objView) {
					UnitNode objNode = (UnitNode) objView.getTag();
					checkCheckBox(objNode, ((CheckBox) objView).isChecked());
					objThis.notifyDataSetChanged();
				}
			});

			objViewHolder.ivIcon = (ImageView) objView
					.findViewById(R.id.ivIcon);
			objViewHolder.tvText = (TextView) objView.findViewById(R.id.tvText);
			objViewHolder.ivFlagEC = (ImageView) objView
					.findViewById(R.id.ivFlagEC);
			objView.setTag(objViewHolder);
		} else {
			objViewHolder = (ViewHolder) objView.getTag();
		}

		// �õ���ǰ�ڵ�
		UnitNode objNode = listAllNode.get(intPosition);

		if (objNode != null) {
			if (boolHasRadioButton) {
				objViewHolder.rbSelect.setTag(objNode);
				objViewHolder.rbSelect.setChecked(objNode.isChecked());
				// �Ƿ���ʾ��ѡ��
				if (objNode.hasRadioButton() && boolHasRadioButton) {
					objViewHolder.rbSelect.setVisibility(View.VISIBLE);
				} else {
					objViewHolder.rbSelect.setVisibility(View.GONE);
				}
			}
			if (boolHasCheckBox) {
				objViewHolder.cbSelect.setTag(objNode);
				objViewHolder.cbSelect.setChecked(objNode.isChecked());
				// �Ƿ���ʾ��ѡ��
				if (objNode.hasCheckBox() && boolHasCheckBox) {
					objViewHolder.cbSelect.setVisibility(View.VISIBLE);
				} else {
					objViewHolder.cbSelect.setVisibility(View.GONE);
				}
			}
			// �Ƿ���ʾͼ��
			if (objNode.getIcon() == -1) {
				objViewHolder.ivIcon.setVisibility(View.GONE);
			} else {
				objViewHolder.ivIcon.setVisibility(View.VISIBLE);
				objViewHolder.ivIcon.setImageResource(objNode.getIcon());
			}

			// ��ʾ�ı�
			objViewHolder.tvText.setText(objNode.getText());

			if (objNode.isLeaf()) {
				// �����Ҷ�ڵ��� ����ʾչ�����۵�״̬ͼ��
				objViewHolder.ivFlagEC.setVisibility(View.GONE);
			} else {
				// ����ʱ�����ӽڵ�չ�����۵�,״̬ͼ��ı�
				objViewHolder.ivFlagEC.setVisibility(View.VISIBLE);
				if (objNode.isExpanded()) {
					if (intIconExpanded != -1)
						objViewHolder.ivFlagEC
								.setImageResource(intIconExpanded);
				} else {
					if (intIconCollapsed != -1)
						objViewHolder.ivFlagEC
								.setImageResource(intIconCollapsed);
				}
			}
			// ��������
			objView.setPadding(40 * objNode.getLevel(), 3, 3, 3);
		}

		return objView;
	}

	/**
	 * �б���ؼ�����
	 */
	public class ViewHolder {
		RadioButton rbSelect;
		CheckBox cbSelect;
		ImageView ivIcon;
		TextView tvText;
		// ���ұߵ�չ�����۵��ı�� ">"��"v"
		ImageView ivFlagEC;
	}

}
